\def\fileversion{0.999 c}
\def\filedate{2010/08/21}
%  Author Alain Matthes
%<--------------------------------------------------------------------------–>
%% Objet :  tools for arithmetic 

%<--------------------------------------------------------------------------–>
%    [2010/04/16 v0.99c integers numbers]
%<--------------------------------------------------------------------------–>
\newcount\tkz@prim@a
\newcount\tkz@prim@b
\newcount\tkzprim@a
\newcount\tkzprim@b
\newcount\tkzprim@r
\newcount\tkzprim@q
\newcount\tkzprim@n
\newcount\tkzprim@d
\newcount\tkzprim@last
\newcount\NbOfPrimFactor

\newif\iftkz@prime
\newif\iftkz@unknown
\newif\iftkz@test@
\newif\iftkzIsAPrim 
\newif\iftkzIsAFactor

\NbOfPrimFactor0 %

% tkz@loop (from plain.tex and pgf)

\def\tkz@loop#1\tkz@repeat{\def\tkz@body{#1}\tkz@iterate}
\def\tkz@iterate{\tkz@body \let\tkz@next\tkz@iterate \else\let\tkz@next\relax\fi \tkz@next}
\let\tkz@repeat=\fi % this makes \loop...\if...\repeat skippable      
%<--------------------------------------------------------------------------–>
%                            arithmetic tools 
%<--------------------------------------------------------------------------–>
%                          Mod
% ne sera plus nécessaire avec pgf 2 cvs
\ifdefined\pgfmathdeclarefunction
\else
\def\pgfmath@parsefunction@Mod{%
  \expandafter\pgfmath@parsefunction@Mod@}
\def\pgfmath@parsefunction@Mod@(#1,{%
  \pgfmathparse@{#1}\edef\pgfmath@firstoperand{\pgfmathresult}%
  \let\pgfmath@parsepostgroup\pgfmath@parsefunction@Mod@@
  \pgfmath@startparsegroup}
\def\pgfmath@parsefunction@Mod@@{%
  \edef\pgfmath@secondoperand{\pgfmathresult}%
  \pgfmathMod@{\pgfmath@firstoperand}{\pgfmath@secondoperand}%
  \pgfmath@postfunction}
% \pgfmathmod
% Calculate #1 Mod #2.
\def\pgfmathMod#1#2{%
  \edef\pgfmath@marshal{%
    \noexpand\pgfmathparse{#2}
    \noexpand\let\noexpand\pgfmath@arg\noexpand\pgfmathresult%
    \noexpand\pgfmathparse{#1}%
  }%
  \pgfmath@marshal%
  \pgfmathMod@{\pgfmathresult}{\pgfmath@arg}%
}
\def\pgfmathMod@#1#2{%
  \begingroup
    \pgfmath@xa#1pt%
    \pgfmath@xb#2pt%
    \pgfmath@x\pgfmath@xa%
    \c@pgfmath@counta\pgfmath@xa%
    \c@pgfmath@countb\pgfmath@xb%
    \divide\c@pgfmath@counta\c@pgfmath@countb%
    \multiply\pgfmath@xb\c@pgfmath@counta%
    \advance\pgfmath@x-\pgfmath@xb%
        \pgfmath@xb#2pt%
        \ifdim\pgfmath@x<0pt\relax%
       \advance\pgfmath@x\pgfmath@xb\relax%
     \fi
    \pgfmath@returnone\pgfmath@x%
  \endgroup
}
\fi
%<--------------------------------------------------------------------------–>
%                    find factors of an integer
%<--------------------------------------------------------------------------–>

\newcommand*{\tkz@factors}[1]{%
\tkzprim@b#1 
\tkzprim@d2\relax
%<--------------------------------------------------------------------------–>
\tkz@test@true 
\@whilesw\iftkz@test@ \fi{%
\ifodd\tkzprim@b  \tkz@test@false 
\else
 \divide\tkzprim@b by2 %
  $\number\tkzprim@d\ $ 
\fi}%
\tkzprim@last=1 %
  \loop  \ifnum\tkzprim@b>1  
    \tkzprim@d\tkzprim@last\relax
    \tkzprim@r1\relax
%<--------------------------------------------------------------------------–>
\tkz@loop\ifnum\tkzprim@r>0 % 
            \tkzprim@last\tkzprim@d
            \advance\tkzprim@d by2\relax% on commence par 3
            \tkzprim@a\tkzprim@b% macro mod !!
            \tkzprim@r\tkzprim@a%
            \divide\tkzprim@a by \tkzprim@d
            \multiply\tkzprim@a by\tkzprim@d %
            \advance\tkzprim@r by-\tkzprim@a%
     \tkz@repeat % fin du whiledo on a le reste
    \divide\tkzprim@b by\tkzprim@d\relax
    $\number\tkzprim@d\ $%  affiche le facteur
\repeat% fin du premier whiledo
}%    
%<--------------------------------------------------------------------------–>
%                  search factors   % if prime number tkzIsAPrimtrue        
%<--------------------------------------------------------------------------–>
\def\tkzfactors#1{%
\tkzprim@b#1 
\tkzIsAPrimfalse
\tkz@primality{\tkzprim@b}% 
\iftkzIsAPrim
   \number\tkzprim@b\ % prime number
\else we can found factors
\tkz@factors{#1} 
\fi
}%
%<--------------------------------------------------------------------------–>
% \iftkzIsAPrim to test
%<--------------------------------------------------------------------------–>
\def\tkz@primality#1{%
\tkz@unknowntrue
\tkzprim@n =#1
\ifnum\tkzprim@n =1 \tkz@primefalse\tkz@unknownfalse\fi
\ifnum\tkzprim@n =2 \tkz@primetrue\tkz@unknownfalse\fi
\ifnum\tkzprim@n =3 \tkz@primetrue\tkz@unknownfalse\fi
\iftkz@unknown
\tkzprim@d=2 
\tkzprim@a=\tkzprim@n  %
     \divide\tkzprim@a by\tkzprim@d %
     \multiply\tkzprim@a by\tkzprim@d %
\ifnum\tkzprim@a=\tkzprim@n  \global\tkz@primefalse\tkz@unknownfalse\fi
 \fi
\iftkz@unknown
\tkzprim@d=3 \global\tkz@primetrue
 \tkz@loop
 \tkzprim@a=\tkzprim@n  \divide\tkzprim@a by\tkzprim@d
  \ifnum\tkzprim@a>\tkzprim@d \tkz@unknowntrue\else\tkz@unknownfalse\fi
    \multiply\tkzprim@a by\tkzprim@d
  \ifnum\tkzprim@a=\tkzprim@n  \global\tkz@primefalse\tkz@unknownfalse\fi
  \iftkz@unknown\advance\tkzprim@d by 2 %
 \tkz@repeat
\fi
\iftkz@prime\tkzIsAPrimtrue\else\tkzIsAPrimfalse\fi
}%
%<--------------------------------------------------------------------------–>
% #2 divide #1 ?
%<--------------------------------------------------------------------------–>
\def\tkzFindFactor#1#2{% 
\tkzprim@a#1 \tkzprim@b#2
\tkzprim@r#1    
\divide\tkzprim@a by\tkzprim@b%
\multiply\tkzprim@a by\tkzprim@b% 
\advance\tkzprim@r by-\tkzprim@a%
\ifnum\tkzprim@r=0 \tkzIsAFactortrue\else\tkzIsAFactorfalse\fi}% 
%<--------------------------------------------------------------------------–>
% find pgcd of #1 and #2 result in \tkz@mathresult and \theprim@a
%<--------------------------------------------------------------------------–>
\def\tkz@pgcd#1#2{%   test si l'un est nul

\tkzprim@a#1 \tkzprim@b#2
\tkz@test@true
\ifnum\tkzprim@a=0 \tkz@test@false\tkzprim@a\tkzprim@b\fi
\ifnum\tkzprim@b=0 \tkz@test@false\tkzprim@b\tkzprim@a\fi  
\@whilesw\iftkz@test@ \fi{%
\ifnum\tkzprim@a=\tkzprim@b\tkz@test@false
\else
 \ifnum\tkzprim@a>\tkzprim@b%
 \advance \tkzprim@a by-\tkzprim@b
\else \advance \tkzprim@b by-\tkzprim@a\fi
\fi}%
\edef\tkz@mathresult{\number\tkzprim@a}}%
%<--------------------------------------------------------------------------–>
%                   Simplification d'une fraction
% #2 if #2<>1 then frac with pi
%<--------------------------------------------------------------------------–>
\def\tkz@reducfrac#1#2{%
\ifnum#1<0\relax%
   \tkz@prim@a 0 
   \advance\tkz@prim@a by -#1
 \else
    \tkz@prim@a#1 %
 \fi
  \tkz@prim@b#2 %
  \tkz@pgcd{\tkz@prim@a}{\tkz@prim@b}% 
 \ifnum\tkz@prim@a=0%
   \tkz@prim@a#1 %
   \tkz@prim@b#2 %
 \else 
   \tkz@prim@a#1 
   \tkz@prim@b#2 
   \divide \tkz@prim@a by\tkz@mathresult   
   \divide \tkz@prim@b by\tkz@mathresult  
\fi
\def\tkz@rfrac{$\frac{\the\tkz@prim@a\pi}{\the\tkz@prim@b}$}
% modif for pi
 \ifnum\tkz@prim@a=0%
    \global\def\tkz@rfrac{}%
 \else
 \ifnum\tkz@prim@b=1 %
    \ifnum\tkz@prim@a=1 %
      \global\def\tkz@rfrac{$\scriptstyle{\pi}$}%
      \else
       \ifnum\tkz@prim@a=-1%
          \global\def\tkz@rfrac{$\scriptstyle{-\pi}$}%
        \else
          \global\def\tkz@rfrac{$\scriptstyle{\the\tkz@prim@a\pi}$}%
       \fi\fi
 \else 
   \ifnum\tkz@prim@a=1 %
       \global\def\tkz@rfrac{$\frac{\pi}{\the\tkz@prim@b}$}%
     \else
       \ifnum\tkz@prim@a=-1 %
          \global\def\tkz@rfrac{$\frac{-\pi}{\the\tkz@prim@b}$}%
        \else 
           \global\def\tkz@rfrac{$\frac{\the\tkz@prim@a\pi}{\the\tkz@prim@b}$}%
 \fi\fi\fi\fi
}%
\endinput


